package ru.hh.team02.chat.client;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.SortedSet;

import org.junit.Test;

public class ClientRegistryConcurrentTests {
    

    @Test
    public void shoudHaveAppropriateClientsCountOnAddingClientsFromMultipleThreads() {
        final int clientCountPerThread = 1000;
        final int threadsCount = 100;
        
        String[] roomNames = { "room1" };
        final ClientRegistry clientRegistry = new ClientRegistry(roomNames);

        final ArrayList<Client> clients =
                new ArrayList<Client>();
        
        for (int i = 0; i < clientCountPerThread; ++i) {
            clients.add(new Client("client" + i));
        }
        
        for (int i = 0; i < threadsCount; ++i) {
            new Thread() {
                @Override
                public void run() {
                    for (int j = 0; j < clientCountPerThread; ++j) {
                        clientRegistry.addClient(clients.get(j), "room1");
                    }
                }
            }.start();    
        }
        
        
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        assertEquals(clientCountPerThread, clientRegistry.getClientsFromRoom("room1").size());

    }

}
